//
//  WalletExtractVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/27.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "WalletExtractVC.h"

@interface WalletExtractVC () {
    int count;
    double mPoint;
}

@property(weak, nonatomic) IBOutlet UIButton *backBt;
@property(weak, nonatomic) IBOutlet UILabel *profitLb;
@property(weak, nonatomic) IBOutlet UIView *cashView;
@property(weak, nonatomic) IBOutlet UITextField *cashTf;
@property(weak, nonatomic) IBOutlet UIButton *allBt;
@property(weak, nonatomic) IBOutlet UIView *accountView;
@property(weak, nonatomic) IBOutlet UITextField *accountTf;
@property(weak, nonatomic) IBOutlet UIButton *scanBt;
@property(weak, nonatomic) IBOutlet UIView *smscodeView;
@property(weak, nonatomic) IBOutlet UITextField *smscodeTf;
@property(weak, nonatomic) IBOutlet UIButton *codeBt;
@property(weak, nonatomic) IBOutlet UIButton *operateBt;

@end

@implementation WalletExtractVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestPoint];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    mPoint = -1;
    self.allBt.layer.cornerRadius = globalRadius;
    self.cashView.layer.cornerRadius = globalRadius;
    self.accountView.layer.cornerRadius = globalRadius;
    self.smscodeView.layer.cornerRadius = globalRadius;
    self.codeBt.layer.cornerRadius = globalRadius;
    [self.cashTf setKeyboardType:UIKeyboardTypeDecimalPad];
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [[self.allBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (self->mPoint == -1) {
            [JPToast showText:@""];
            [self requestPoint];
        } else if (self->mPoint == 0) {
            NSString *msg = @"\n\n您还没有可提取的收益哦\n\n";
            UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [vc addAction:cancel];
            [self presentViewController:vc animated:YES completion:nil];
        } else {
            self.cashTf.text = [[NSNumber numberWithDouble:self->mPoint] stringValue];
        }
    }];
    [[self.scanBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ScannerVC *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"ScannerVCID"];
        [vc setHint:@"将扫描框对准账户二维码" block:^(NSString *result) {
            self.accountTf.text = result;
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.codeBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.cashTf resignFirstResponder];
        [self.accountTf resignFirstResponder];
        [self.smscodeTf resignFirstResponder];
        NSString *account = self.accountTf.text;
        if (account<=0) {
            [JPToast showText:@"请输入钱包地址"];
            return;
        }
        [self requestSms];
    }];
    [[self.operateBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.cashTf resignFirstResponder];
        [self.accountTf resignFirstResponder];
        [self.smscodeTf resignFirstResponder];
        NSString *cash = self.cashTf.text;
        NSString *account = self.accountTf.text;
        NSString *smscode = self.smscodeTf.text;
        if (cash.length<=0 || [cash doubleValue] == 0) {
            [JPToast showText:@"请输入本次提现金额"];
            return;
        }
        if (account<=0) {
            [JPToast showText:@"请输入钱包地址"];
            return;
        }
        if (![self isWalletAddress:account]) {
            [JPToast showText:@"输入的钱包地址不正确"];
            return;
        }
        if (smscode<=0) {
            [JPToast showText:@"请输入短信验证码"];
            return;
        }
        [self requestExtract];
    }];
}

- (void)requestPoint {
    //账户余额
    [[[HttpRequest shareInstance] signGet:w_point map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            @try {
                id data = x[@"data"];
                if (data[@"point"]) {
                    self->mPoint = [data[@"point"] doubleValue];
                    self.profitLb.text = [NSString stringWithFormat:@"%@",data[@"point"]];
                }
            } @catch (NSException *exception) {
                
            }
        }
    }];
}

- (void)requestSms {
    [self showLoading:@"正在获取验证码"];
    [[[HttpRequest shareInstance] signGet:w_extract_sms map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            self->count = 60;
            [self countDown];
            [JPToast showText:x[@"message"]];
        } else {
            NSString *msg = x[@"message"];
            msg = [msg containsString:@"流控Permits"] ? @"您的验证操作过于频繁，请稍后再试" : msg;
            [JPToast showText:msg];
            [self.codeBt setEnabled:YES];
        }
        [self hideLoading];
    } error:^(NSError *error) {
        [self.codeBt setEnabled:YES];
        [JPToast showText:@"网络错误"];
        [self hideLoading];
    }];
}

- (void)requestExtract {
    NSDictionary *dic = @{@"point":self.cashTf.text, @"smsCode":self.smscodeTf.text, @"address":self.accountTf.text};
    [[[HttpRequest shareInstance] signPost:w_extract map:[NSMutableDictionary dictionaryWithDictionary:dic] ] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            [JPToast showText:@"提现成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
    }];
}

- (void)countDown {
    if (count<=0) {
        [self.codeBt setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.codeBt setEnabled:YES];
    } else {
        [self.codeBt setTitle:[NSString stringWithFormat:@"%dS",count] forState:UIControlStateNormal];
        [self.codeBt setEnabled:NO];
        count--;
        [self performSelector:@selector(countDown) withObject:nil afterDelay:1];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
